package cn.jdy.dzdd.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.anquan.service.CompanySafeService;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.dzdd.service.DzddAssociationsService;
import cn.jdy.dzdd.service.DzddNewsService;
import cn.jdy.dzdd.service.DzddRankingService;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.service.AdviceService;
import cn.jdy.plugins.service.BulletinService;
import cn.jdy.plugins.service.CitiesService;
import cn.jdy.plugins.service.CompanyService;
import cn.jdy.plugins.service.DocService;
import cn.jdy.plugins.service.NewspService;
import cn.jdy.plugins.service.RemoteAssociationsService;
import cn.jdy.plugins.service.ResearchService;
import cn.jdy.plugins.service.WorksService;

/**
 * @desc 网站首页
 * @author ningqian
 * @date   2016-10-26
 */
@RestController
@RequestMapping("/")
public class DzddIndexCtrl extends BaseCtrl {

	@Autowired
	private CompanySafeService companySafeService; 
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
    private DzddNewsService newsService;
	
	@Autowired
    private NewspService newspService;
	
	@Autowired
    private DocService docService;
	
	@Autowired
	  private DzddAssociationsService associationsService;
	
	@Autowired
	  private RemoteAssociationsService remoteAssociationsService;
	
	@Autowired
	  private WorksService worksService;
	
	@Autowired
	  private BulletinService bulletinService;
	
	@Autowired
	  private ResearchService researchService;
	
	@Autowired
	  private AdviceService adviceService;
	@Autowired
	private CitiesService citiesService;
	@Autowired
	private DzddRankingService dzddRankingService;
	@Autowired
	private ConfService confService;
	/**
   * 首页-处理老链接
   */
//  @RequestMapping("index.html")
//  public ModelAndView forIndexold() {
//    return new ModelAndView("redirect:");// 将之前index.html 的权重传递给域名
//  }
// 

	  /**
	   * 临时
		 * 首页
		 */
		@RequestMapping("/index")
		public ModelAndView indexes() {
			ModelAndView mv = new ModelAndView("/index.jsp");
			return mv;
		}
			
	/**
	 * 东津公安分局
	 * 
	 * 首页
	 */
	@RequestMapping({"","index.html"})
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("/index.jsp");
		HttpServletRequest req = (HttpServletRequest) request;
		String ip = String.valueOf(req.getRemoteAddr());
		String ip1=ip.substring(0,ip.lastIndexOf("."));//取ip前三位
		List<ConfValue> list = dzddRankingService.getremarkip();
		for (ConfValue c : list) {
			if(c.getRemark().length()>0&&c.getValue1().length()==0){
				
				if(c.getRemark().equals(ip)){
					dzddRankingService.updateremark(c.getId());
				}
			}else if(c.getRemark().length()>0&&c.getValue1().length()>0){
				String str0=c.getRemark().substring(0,ip.lastIndexOf("."));
				String str1=c.getRemark();
				String str2=c.getValue1();
				int i0 =Integer.parseInt( ip.substring(ip.lastIndexOf(".")+1));
				int i1 =Integer.parseInt( str1.substring(str1.lastIndexOf(".")+1));
				int i2 = Integer.parseInt(str2.substring(str2.lastIndexOf(".")+1));
				if(ip1.equals(str0)){
					if (i0>=i1&&i0<=i2) {
						dzddRankingService.updateremark(c.getId());	
					}
					}
				}
			}			

		
			
		
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);//日一二三四五六=0123456
		mv.addObject("datalist01",dzddRankingService.querySiteMap01());//投稿排名（队属单位）
		mv.addObject("datalist",dzddRankingService.querySiteMap());//投稿排名（机关科室）
//		mv.addObject("datalist1",dzddRankingService.querySiteMap1());//投稿排名（派出所）
//		mv.addObject("datalist11",dzddRankingService.querySiteMap11());//投稿排名（派出所）
//		mv.addObject("data0",dzddRankingService.queryremarkMap());//浏览排名（局直）
//		mv.addObject("data01",dzddRankingService.queryremarkMap01());//浏览排名（局直）
//		mv.addObject("data1",dzddRankingService.queryremarkMap1());//浏览排名（派出所）
//		mv.addObject("data11",dzddRankingService.queryremarkMap11());//浏览排名（派出所）
		mv.addObject("get_id",dzddRankingService.get_id());     //返回最新一条今日值班页面
		
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String time=format.format(date);
		String aa=time.toString().substring(8, 10);

		request.setAttribute("systime", aa);
		
		return mv;
	}


	
	  
	
	  /**
	   * 有效
	   * 市局
	   * 更多专题
	   */
	  @RequestMapping("more")
	  public ModelAndView loadMores() {
	    ModelAndView mv = new ModelAndView("/moreList.jsp");
	  //襄阳市公安局，需要加入当前时间段周数
	  		Date datetime = new Date();
	  		int weekdays = datetime.getDay();
	  		mv.addObject("weekdays",weekdays);
	  		
	    return mv;
	  }
	  
	  /**
	   * 有效
	   * 市局
	   * 更多应用系统
	   */
	  @RequestMapping("moreApps")
	  public ModelAndView loadMoreApps() {
	    ModelAndView mv = new ModelAndView("/appsList.jsp");
	  //襄阳市公安局，需要加入当前时间段周数
	  		Date datetime = new Date();
	  		int weekdays = datetime.getDay();
	  		mv.addObject("weekdays",weekdays);
	  		
	    return mv;
	  }
	  
	/**
	 * 清除掉全部数据缓存
	 * @return
	 */
	@RequestMapping("/clearCache")
	@CacheEvict(value = "cache1",allEntries=true)
	public String clearCache(){
	  return "ok...";
	}

  /**
   * 认证页面
   */
  @RequestMapping("verify")
  public ModelAndView verifyIndex() {
    ModelAndView mv = new ModelAndView("/verify.jsp");
    return mv;
  }
  
  /**
   * 个人中心 图片上传 upfileImg.jsp
   */
  @RequestMapping("upfileImg")
  public ModelAndView upfileImg() {
    ModelAndView mv = new ModelAndView("/upfileImg.jsp");
    return mv;
  }

  /**
   * 图片上传公共 upfileImg2.jsp
   */
  @RequestMapping("upfileImg2")
  public ModelAndView upfileImg2() {
    ModelAndView mv = new ModelAndView("/upfileImg2.jsp");
    return mv;
  }
  
  /**
   * 404页面
   */
  @RequestMapping("404")
  public ModelAndView notfond() {
    return new ModelAndView("/404.jsp");
  }
  /**
   * 500页面
   */
  @RequestMapping("500")
  public ModelAndView servicebusy() {
    return new ModelAndView("/500.jsp");
  }
  
  /**
   * 正在建设中页面
   */
  @RequestMapping("waiting")
  public ModelAndView waiting() {
    return new ModelAndView("/waiting.jsp");
  }

  
}
